package com.idbk.chargestation.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import com.android.volley.toolbox.StringRequest;
import com.idbk.chargestation.AppContext;
import com.idbk.chargestation.R;
import com.idbk.chargestation.activity.user.ActivityLogin;
import com.idbk.chargestation.bean.JsonBase;
import com.idbk.chargestation.util.GlobalResult;

/**
 * Fragment的轻封装，处理公共的一些逻辑
 * Created by zhx on 2017/8/30.
 */

public class BaseFragment extends Fragment {

    protected StringRequest mMainRequest;
    protected ProgressDialog mMainProgressDialog;


    protected void showMyProgressDialog(Context context, String msg){
        mMainProgressDialog = ProgressDialog.show(context, null, msg, true, true,
                new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                            cancelCurrentRequest();
                    }
                });
        mMainProgressDialog.setCanceledOnTouchOutside(false);
    }

    protected void cancelCurrentRequest(){
        if(mMainRequest != null){
            mMainRequest.cancel();
        }
    }

    protected void dismissMyProgressDialog(){
        if(mMainProgressDialog != null){
            mMainProgressDialog.dismiss();
        }
    }

    public static boolean handleResponseStatus(Context context, JsonBase result){
        if(result == null){
            Toast.makeText(context, R.string.common_tip_rusultzero, Toast.LENGTH_SHORT).show();
            return false;
        } else if(result.status == GlobalResult.OK.getStatus()) {
            return true;
        } else if(needLogin(result)) {
            showLoginDialog(context);
            return false;
        } else {
            Toast.makeText(context, result.message, Toast.LENGTH_SHORT).show();
            return false;
        }
    }

    private static boolean needLogin(JsonBase result) {
        return result.status == GlobalResult.TOKEN_INVALID.getStatus()
                || result.status == GlobalResult.TOKEN_REQUIRED.getStatus();
    }

    private static void showLoginDialog(final Context context) {
        AppContext.getInstance().clearLoginState();//清除登录状态
        new AlertDialog.Builder(context)
                .setTitle(R.string.common_dialog_tip)
                .setMessage("登录超时，请重新登录！")
                .setCancelable(false)
                .setPositiveButton(R.string.common_ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(context, ActivityLogin.class);
                        context.startActivity(intent);
                    }
                })
                .create()
                .show();
    }

}
